package com.sky.wsp.rabbitmq.exchange;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * topic交换机模型，需要一个topic交换机，两个队列和三个binding
 */
@Configuration
public class TopicExchangeConfig {
    public static final String EXCHANGE_NAME = "my_topic";
    public static final String QUEUE_NAME_1 = "topic_queue_1";
    public static final String QUEUE_NAME_2 = "topic_queue_2";

    public static final String  ROUTING_KEY_1 = "USER.*";
    public static final String  ROUTING_KEY_2 = "*.UPDATE";
    public static final String  ROUTING_KEY_3 = "ORG.#";

    @Bean
    public TopicExchange topicExchange() {
        TopicExchange topicExchange = new TopicExchange(EXCHANGE_NAME);
        return topicExchange;
    }

    @Bean
    public Queue topicQueue1() {
        Queue queue = new Queue(QUEUE_NAME_1);
        return queue;
    }

    @Bean
    public Queue topicQueue2() {
        Queue queue = new Queue(QUEUE_NAME_2);
        return queue;
    }

    //3个binding将交换机和相应队列连起来
    @Bean
    public Binding bindingTopic1() {
        //#表示0个或若干个关键字，*表示一个关键字
        Binding binding = BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(ROUTING_KEY_1);
        return binding;
    }

    @Bean
    public Binding bindingTopic2() {
        //#表示0个或若干个关键字，*表示一个关键字
        Binding binding = BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ROUTING_KEY_2);
        return binding;
    }

    @Bean
    public Binding bindingTopic3() {
        //#表示0个或若干个关键字，*表示一个关键字
        Binding binding = BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ROUTING_KEY_3);
        return binding;
    }
}
